<?php

namespace App\Console\Commands\WorkOrder;

use App\Enums\WorkOrder\OrderEnum;
use App\Models\WorkOrder\WorkOrderApplyContentModel;
use App\Models\WorkOrder\WorkOrderApplyMainModel;
use App\Service\WorkOrder\ProblemFeedbackService;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;

class InitErrGoodsTagName extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'workOrder:initErrGoodsTagName';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '初始化品质工单-问题产品增加商品 统一名称';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        WorkOrderApplyMainModel::query()
            ->with(['contentRel'])
            ->where('problemPid', OrderEnum::PROBLEM_PID_2)                                 // 品质异常问题
            ->whereIn('status', [OrderEnum::MAIN_STATUS_ING, OrderEnum::MAIN_STATUS_FINISH])// 工单状态
            ->chunk(
                100, function ($applyMain) {
                foreach ($applyMain as $main) {
                    $content        = json_decode($main->contentRel->content ?? '', true) ?? [];
                    $ormOrderIds    = $content['bookNo'] ?? [];                                 // 关联订单号
                    $errJdeCodesOld = $content['errJdeCodes'] ?? [];                            // 选择的问题产品
                    $goodsCode      = array_unique(array_column($errJdeCodesOld, 'goods_code'));// 产品code

                    if ($ormOrderIds && is_array($ormOrderIds) && $errJdeCodesOld && $goodsCode) {
                        $errJdeCodesNew = [];
                        foreach ($ormOrderIds as $ormOrderId) {
                            $ormOrderGoods = ProblemFeedbackService::getOrmOrderGoods($ormOrderId);// 通过订单号查询产品
                            if (!$ormOrderGoods) {
                                continue;
                            }
                            foreach ($ormOrderGoods as $v) {
                                if (in_array($v['goods_code'], $goodsCode)) {
                                    $errJdeCodesNew[] = $v;
                                }
                            }
                        }
                        if ($errJdeCodesNew) {
                            $content['errJdeCodes'] = $errJdeCodesNew;

                            Log::info(
                                'initErrGoodsTagName', [
                                    'workOrderNo'    => $main->workOrderNo,
                                    'errJdeCodesOld' => $errJdeCodesOld,
                                    'errJdeCodesNew' => $errJdeCodesNew,
                                    'content'        => $content,
                                ]
                            );

                            // 修改工单详情-选择的问题产品
                            WorkOrderApplyContentModel::query()
                                ->where('id', $main->contentRel->id)
                                ->update(['content' => json_encode($content, JSON_UNESCAPED_UNICODE)]);
                        }
                    }
                }
            }
            );
    }
}
